package com.roc.steelend.service.Impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.roc.steelend.controller.vo.account.AccountSpecialSaleOrderIdDetail;
import com.roc.steelend.controller.vo.account.AccountSpecialSaleReception;
import com.roc.steelend.controller.vo.invoice.InvoiceBySaleOrder;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceByKaipingReception;
import com.roc.steelend.controller.vo.specialInvoice.SpecialInvoiceBySaleReception;
import com.roc.steelend.controller.vo.specialPay.SpecialBySaleReception;
import com.roc.steelend.mapper.*;
import com.roc.steelend.pojo.Account;
import com.roc.steelend.pojo.Client;
import com.roc.steelend.pojo.CompanyMoney;
import com.roc.steelend.pojo.Invoice;
import com.roc.steelend.service.AccountService;
import com.roc.steelend.service.ClientService;
import com.roc.steelend.service.InvoiceService;
import com.roc.steelend.util.LshUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class ClientServiceImpl implements ClientService {
    @Autowired
    private ClientMapper clientMapper;

    @Autowired
    private AccountService accountService;
    @Autowired
    private CompanyMoneyMapper companyMoneyMapper;
    @Autowired
    private SaleOrderMapper saleOrderMapper;

    @Autowired
    private InvoiceService invoiceService;
    @Autowired
    private InvoiceMapper invoiceMapper;
    @Autowired
    private AccountMapper accountMapper;


    @Override
    public List<HashMap<String, Object>> findClientList(Integer groupId, String searchMsg, Integer now, Integer size) {


        now=(now-1)*size;

        if (searchMsg==null){

        }else {
            searchMsg="%"+searchMsg+"%";
        }

        return clientMapper.findClientList(groupId,searchMsg,now,size);

    }

    @Override
    public Integer countClient(Integer groupId, String searchMsg) {
        QueryWrapper<Client> wrapper = new QueryWrapper<>();
        if (groupId!=null && groupId != 0){
            wrapper.eq("group_id",groupId);
        }
        if (searchMsg!=null){
            wrapper.like("client_name","%"+searchMsg+"%");
        }
        Integer count = clientMapper.selectCount(wrapper);
        return count;
    }

    @Override
    public Boolean clientRegistered(Client client) {
        client.setDebt(new BigDecimal("0"));
        client.setInvoice(new BigDecimal("0"));
        client.setSignupTime(new Timestamp(new Date().getTime()));
        int insert = clientMapper.insert(client);
        if (insert==1){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public Client getClientData(Integer clientId) {

        QueryWrapper<Client> wrapper = new QueryWrapper<>();
        wrapper.eq("client_id",clientId);

        return  clientMapper.selectList(wrapper).get(0);
    }

    @Override
    public ReturnMsg postAccountClient(SpecialBySaleReception specialBySaleReception,Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
        List<HashMap<String, Object>> hashMaps = saleOrderMapper.selectAllBuClientId(specialBySaleReception.getClientId());
        if (hashMaps.isEmpty()){
            Account account = new Account();
            account.setAccountId(LshUtil.getAccountSpecialLsh());
            account.setEmployeeId(employeeId);
            account.setPayTakeId(specialBySaleReception.getPayOrTakeWay());
            account.setSmallAccountId(1);
            account.setOperateTime(new Timestamp(new Date().getTime()));
            account.setAccountTime(specialBySaleReception.getPayTime());
            account.setMoney(specialBySaleReception.getPayMoney());
            account.setWho(specialBySaleReception.getClientName());
            if (Objects.equals(specialBySaleReception.getRemark(), "")){
                specialBySaleReception.setRemark(null);
            }
            account.setRemark(specialBySaleReception.getRemark());
            account.setWay(1);
            int insert = accountMapper.insert(account);
            QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
            clientQueryWrapper.eq("client_id",specialBySaleReception.getClientId());
            Client client = clientMapper.selectList(clientQueryWrapper).get(0);
            if (specialBySaleReception.getIsOver()){
                client.setDebt(new BigDecimal("0"));
            }else {
                client.setDebt(client.getDebt().subtract(specialBySaleReception.getPayMoney()));
            }
            int i = clientMapper.updateById(client);

            QueryWrapper<CompanyMoney> companyMoneyQueryWrapper = new QueryWrapper<>();
            companyMoneyQueryWrapper.eq("pay_take_id",specialBySaleReception.getPayOrTakeWay());
            CompanyMoney companyMoney = companyMoneyMapper.selectList(companyMoneyQueryWrapper).get(0);
            companyMoney.setMoney(companyMoney.getMoney().add(specialBySaleReception.getPayMoney()));
            int i1 = companyMoneyMapper.updateById(companyMoney);
            if (insert==1&&i==1&&i1==1){
                msg.setCode(1);
                msg.setMsg("销售收款账务操作成功");

            }else {
                msg.setCode(0);
                msg.setMsg("销售收款账务操作失败");
            }
            return msg;
        }else {
            ArrayList<AccountSpecialSaleOrderIdDetail> accountSpecialSaleOrderIdDetails = new ArrayList<>();
            for (int i = 0; i <hashMaps.size() ; i++) {
                HashMap<String, Object> hashMap = hashMaps.get(i);
                String sale_order_id = hashMap.get("sale_order_id").toString();
                BigInteger bigInteger = new BigInteger(sale_order_id);
                java.sql.Date out_storage_date = (java.sql.Date)hashMap.get("out_storage_date");
                AccountSpecialSaleOrderIdDetail accountSpecialSaleOrderIdDetail = new AccountSpecialSaleOrderIdDetail(bigInteger, out_storage_date);
                accountSpecialSaleOrderIdDetails.add(accountSpecialSaleOrderIdDetail);
            }
            AccountSpecialSaleReception accountSpecialSaleReception = new AccountSpecialSaleReception();
            accountSpecialSaleReception.setClientId(specialBySaleReception.getClientId());
            accountSpecialSaleReception.setSaleOrderIdList(accountSpecialSaleOrderIdDetails);
            accountSpecialSaleReception.setClientName(specialBySaleReception.getClientName());
            if (Objects.equals(specialBySaleReception.getRemark(), "")){
                specialBySaleReception.setRemark(null);
            }
            accountSpecialSaleReception.setRemark(specialBySaleReception.getRemark());
            accountSpecialSaleReception.setPayOrTakeWay(specialBySaleReception.getPayOrTakeWay());
            accountSpecialSaleReception.setPayTime(specialBySaleReception.getPayTime());
            accountSpecialSaleReception.setShouldGiveMoney(specialBySaleReception.getDebt());
            accountSpecialSaleReception.setPayMoney(specialBySaleReception.getPayMoney());
            accountSpecialSaleReception.setIsOver(specialBySaleReception.getIsOver());
            return accountService.postAccountSpecialSale(accountSpecialSaleReception,employeeId);
        }


    }

    @Override
    public ReturnMsg postInvoiceClient(SpecialInvoiceBySaleReception specialInvoiceBySaleReception, Integer employeeId) {
        ReturnMsg msg = new ReturnMsg();
        List<HashMap<String, Object>> hashMaps = saleOrderMapper.selectAllBuClientIdInvoice(specialInvoiceBySaleReception.getClientId());
        if (hashMaps.isEmpty()){
            Invoice invoice = new Invoice();
            invoice.setInvoiceId(LshUtil.getInvoiceSale());
            invoice.setInvoiceType("销售开票(特殊)");
            invoice.setEmployeeId(employeeId);
            invoice.setOperateTime(new Timestamp(new Date().getTime()));
            invoice.setInvoiceTime(specialInvoiceBySaleReception.getInvoiceTime());
            invoice.setInvoiceMoney(specialInvoiceBySaleReception.getInvoiceMoney());
            invoice.setWho(specialInvoiceBySaleReception.getClientName());
            if (Objects.equals(specialInvoiceBySaleReception.getRemark(), "")){
                specialInvoiceBySaleReception.setRemark(null);
            }
            invoice.setRemark(specialInvoiceBySaleReception.getRemark());
            if (Objects.equals(specialInvoiceBySaleReception.getInvoiceTitle(),"")){
                specialInvoiceBySaleReception.setInvoiceTitle(null);
            }
            invoice.setInvoiceTitle(specialInvoiceBySaleReception.getInvoiceTitle());
            int insert = invoiceMapper.insert(invoice);
            QueryWrapper<Client> clientQueryWrapper = new QueryWrapper<>();
            clientQueryWrapper.eq("client_id",specialInvoiceBySaleReception.getClientId());
            Client client = clientMapper.selectList(clientQueryWrapper).get(0);
            if (specialInvoiceBySaleReception.getIsOver()){
                client.setInvoice(new BigDecimal("0") );

            }else {
                client.setInvoice(client.getInvoice().subtract(specialInvoiceBySaleReception.getInvoiceMoney()));

            }

            int i = clientMapper.updateById(client);
            if (insert==1&&i==1){
                msg.setCode(1);
                msg.setMsg("销售开票操作成功");

            }else {
                msg.setCode(0);
                msg.setMsg("销售开票操作失败");
            }
            return msg;

        }else {
            ArrayList<AccountSpecialSaleOrderIdDetail> accountSpecialSaleOrderIdDetails = new ArrayList<>();
            for (int i = 0; i <hashMaps.size() ; i++) {
                HashMap<String, Object> hashMap = hashMaps.get(i);
                String sale_order_id = hashMap.get("sale_order_id").toString();
                BigInteger bigInteger = new BigInteger(sale_order_id);
                java.sql.Date out_storage_date = (java.sql.Date)hashMap.get("out_storage_date");
                AccountSpecialSaleOrderIdDetail accountSpecialSaleOrderIdDetail = new AccountSpecialSaleOrderIdDetail(bigInteger, out_storage_date);
                accountSpecialSaleOrderIdDetails.add(accountSpecialSaleOrderIdDetail);
            }
            InvoiceBySaleOrder invoiceBySaleOrder = new InvoiceBySaleOrder();
            invoiceBySaleOrder.setSaleOrderIdList(accountSpecialSaleOrderIdDetails);
            if (Objects.equals(specialInvoiceBySaleReception.getRemark(), "")){
                specialInvoiceBySaleReception.setRemark(null);
            }
            if (Objects.equals(specialInvoiceBySaleReception.getInvoiceTitle(),"")){
                specialInvoiceBySaleReception.setInvoiceTitle(null);
            }
            invoiceBySaleOrder.setRemark(specialInvoiceBySaleReception.getRemark());
            invoiceBySaleOrder.setClientId(specialInvoiceBySaleReception.getClientId());
            invoiceBySaleOrder.setClientName(specialInvoiceBySaleReception.getClientName());
            invoiceBySaleOrder.setShouldInvoiceMoney(specialInvoiceBySaleReception.getNoInvoice());
            invoiceBySaleOrder.setInvoiceMoney(specialInvoiceBySaleReception.getInvoiceMoney());
            invoiceBySaleOrder.setIsOver(specialInvoiceBySaleReception.getIsOver());
            invoiceBySaleOrder.setInvoiceTime(specialInvoiceBySaleReception.getInvoiceTime());
            invoiceBySaleOrder.setInvoiceTitle(specialInvoiceBySaleReception.getInvoiceTitle());

            return invoiceService.invoiceSale(invoiceBySaleOrder,employeeId);


        }
    }

}
